package com.seecen.cywbackend.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.seecen.cywbackend.mapper.FollowsMapper;
import com.seecen.cywbackend.pojo.Follows;
import com.seecen.cywbackend.pojo.Group;
import com.seecen.cywbackend.pojo.vo.GroupSearchVO;
import com.seecen.cywbackend.pojo.vo.GroupVO;
import com.seecen.cywbackend.service.GroupService;
import com.seecen.cywbackend.mapper.GroupMapper;
import com.seecen.cywbackend.utils.Result;
import jakarta.annotation.Resource;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.util.HashMap;
import java.util.List;

/**
* @author lenovo
* @description 针对表【t_group(圈子表)】的数据库操作Service实现
* @createDate 2025-03-21 16:29:02
*/
@Service
public class GroupServiceImpl extends ServiceImpl<GroupMapper, Group>
    implements GroupService{
    @Resource
    private GroupMapper groupMapper;
    @Resource
    private FollowsMapper followsMapper;

    @Override
    public Result getGroupDetail(Integer groupId, Long userId) {
        LambdaQueryWrapper<Group> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(Group::getId, groupId);
        Group group = groupMapper.selectOne(wrapper);
        GroupVO groupVO = new GroupVO();
        groupVO.setId(group.getId());
        groupVO.setName(group.getName());
        groupVO.setAvatar(group.getAvatar());
        groupVO.setPostCount(group.getPostCount());
        groupVO.setMemberCount(group.getMemberCount());
        //查询关注状态
        LambdaQueryWrapper<Follows> followWrapper = new LambdaQueryWrapper<>();
        followWrapper.eq(Follows::getUserId, userId)
                .eq(Follows::getTargetId, groupId)
                .eq(Follows::getTargetType, 2);
        groupVO.setIsJoined(followsMapper.exists(followWrapper));
        return Result.ok(groupVO);
    }

    @Override
    public Result searchGroups(String keyword, Long userId) {
        // 1. 查询基础圈子信息
        LambdaQueryWrapper<Group> wrapper = new LambdaQueryWrapper<>();
        wrapper.like(StringUtils.hasText(keyword), Group::getName, keyword)
                .or()
                .like(StringUtils.hasText(keyword), Group::getDescription, keyword);

        List<Group> groups = groupMapper.selectList(wrapper);
        List<GroupSearchVO> groupVO = groups.stream().map(group -> {
            GroupSearchVO vo = new GroupSearchVO();
            vo.setId(group.getId());
            vo.setAvatar(group.getAvatar());
            vo.setName(group.getName());
            vo.setDescription(group.getDescription());
            // 查询关注状态
            LambdaQueryWrapper<Follows> followWrapper = new LambdaQueryWrapper<>();
            followWrapper.eq(Follows::getUserId, userId)
                    .eq(Follows::getTargetId, group.getId())
                    .eq(Follows::getTargetType, 2);
            vo.setIsFollowed(followsMapper.exists(followWrapper));
            return vo;
        }).toList();
        return Result.ok(groupVO);
    }
}




